package com.lanou.musicproject.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.musicproject.R;
import com.lanou.musicproject.adapter.MoreNewSongAdapter;
import com.lanou.musicproject.bean.MoreNewSong;

import java.util.List;

/**
 * Created by 王世美 on 15/10/29.
 * 最新歌曲页面
 */
public class MoreSongActivity extends Activity{
    private ImageView backIV;
    private List<MoreNewSong.PlazeAlbumListEntity.RMEntity.AlbumListEntity.ListEntity> data;
    private RequestQueue requestQueue;
    private String path = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&" +
            "method=baidu.ting.plaza.getRecommendAlbum&format=json&offset=0&limit=50";
    private GridView moreSongGV;
    private MoreNewSongAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_song);
        moreSongGV = (GridView) findViewById(R.id.moreSongGV);
        requestQueue = Volley.newRequestQueue(this);
        backIV = (ImageView) findViewById(R.id.backIV);
        backIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        moreSongGV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MoreNewSong.PlazeAlbumListEntity.RMEntity.AlbumListEntity.ListEntity entity = (
                        MoreNewSong.PlazeAlbumListEntity.RMEntity.AlbumListEntity.ListEntity)
                        parent.getItemAtPosition(position);
                Intent intent = new Intent(MoreSongActivity.this, MoreSongInfoActivity.class);
                intent.putExtra("album_id", entity.getAlbum_id());
                startActivity(intent);
            }
        });
        this.addData();

    }
    private void addData() {
        final String getUrl = path;
        StringRequest request = new StringRequest(getUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response != null) {
                    Gson gson = new Gson();
                    MoreNewSong result = gson.fromJson(response,MoreNewSong.class);
                    if (result != null && (data = result.getPlaze_album_list().
                            getRM().getAlbum_list().getList()) != null) {
                        adapter = new MoreNewSongAdapter(MoreSongActivity.this,data);
                        moreSongGV.setAdapter(adapter);
                    }
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
    });
        requestQueue.add(request);
    }
}
